/*
 * Copyright 1999,2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.webduff.methods;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.webduff.WebdavStatus;
import com.googlecode.webduff.exceptions.AccessDeniedException;
import com.googlecode.webduff.exceptions.MethodResponseError;
import com.googlecode.webduff.exceptions.ObjectAlreadyExistsException;
import com.googlecode.webduff.exceptions.WebdavException;
import com.googlecode.webduff.io.URI;
import com.googlecode.webduff.locking.AcquiredLock;
import com.googlecode.webduff.locking.LockFailedException;
import com.googlecode.webduff.locking.ResourceLocks;

import java.io.IOException;

public class DoMkcol extends DeterminableMethod {

	@SuppressWarnings("unused")
	private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DoMkcol.class);

    public MethodResponse execute(HttpServletRequest req, HttpServletResponse resp) throws IOException, MethodResponseError {
        URI theURI = getURIForRequest(req);
        MethodResponse theResponse = new MethodResponse(resp);
        AcquiredLock acquiredLock = null;
        
        try {
			acquiredLock = acquireExclusiveLock(theURI, ResourceLocks.FLAT, req);
		} catch (LockFailedException e) {
			throw new MethodResponseError(WebdavStatus.SC_LOCKED);
		}
		
		URI parentURI = theURI.stripLast();

		try {
            if (store.isFolder(parentURI)) {
                try {
                    if(!store.objectExists(theURI)) {
                    	store.createFolder(theURI);
                    	theResponse.setStatusCode(WebdavStatus.SC_CREATED);
                    } else {
                    	throw new ObjectAlreadyExistsException();
                    }
                } catch(ObjectAlreadyExistsException e) {
                	// object already exists
                    throw new MethodResponseError(
                    	theResponse.setStatusCode(WebdavStatus.SC_METHOD_NOT_ALLOWED).setHeader("Allow", determineMethodsAllowed(store, theURI))
                    );
                }
            } else {
                throw new MethodResponseError(WebdavStatus.SC_CONFLICT);
            }
        } catch (AccessDeniedException e) {
        	throw new MethodResponseError(WebdavStatus.SC_FORBIDDEN);
        } catch (WebdavException e) {
        	throw new MethodResponseError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        } finally {
            acquiredLock.unlock();
        }
		
        return theResponse;
    }
    
}